package com.ycyun.admin.service.impl;

import com.ycyun.admin.dao.SysLogDao;
import com.ycyun.admin.model.SysLogs;
import com.ycyun.admin.model.SysUser;
import com.ycyun.admin.service.SysLogService;
import com.ycyun.admin.utils.UserUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: 哦谢特谢特
 * @Date: 2018/5/21 19:06
 * @Description:
 */
@Service
public class SysLogServiceImpl implements SysLogService{

    private static final Logger log = LoggerFactory.getLogger("adminLogger");

    @Autowired
    private SysLogDao sysLogDao;

    @Override
    public void save(SysLogs sysLogs) {

        SysUser user = UserUtil.getLoginUser();
        if (user == null || user.getId() == null) {
            return;
        }
        sysLogs.setUser(user);
        sysLogDao.save(sysLogs);
    }
    @Async
    @Override
    public void save(Long userId, String module, Boolean flag, String remark) {

        SysUser sysUser = new SysUser();
        sysUser.setId(userId);

        SysLogs sysLogs = new SysLogs();
        sysLogs.setFlag(flag);
        sysLogs.setModule(module);
        sysLogs.setRemark(remark);
        sysLogs.setUser(sysUser);

        sysLogDao.save(sysLogs);
    }

    @Override
    public void deleteLogs() {

        Date date = DateUtils.addMonths(new Date(),-3);
        String time = DateFormatUtils.format(date, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());


        int n = sysLogDao.deleteLogs(time);
        log.info("删除{}之前的{}条日志",time,n);


    }
}
